home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FH-3DTUT.ZIP / TUT2.PAS < prev    next >
Pascal/Delphi Source File  |  1995-12-23  |  3KB  |  69 lines

  1. program Tutorial2; {to get the basics take a look at TUT1.PAS}
  2. {Cube Rotation
  3.  Made by fh94.3 (C) 12/23/1995
  4.  Mail: fh@viktoria.drp.fmph.uniba.sk
  5.  
  6.  Based on explanation by Synergist (rhr0982@grace.rit.edu).
  7. }
  8. uses tutunit,crt;
  9.  
  10. const cube: array[0..23] of real = {8 points * 3 coords(X,Y,Z) = 24 numbers}
  11.     (-1,-1,-1,-1,-1,1,-1,1,-1,
  12.     -1,1,1,1,-1,-1,1,-1,1,
  13.     1,1,-1,1,1,1); {coords of the objects (like point1_x, point1_y, point1_z,
  14.                     point2_x, point2_y, ...)}
  15. const lindex: array[0..23] of integer = {12 lines * 2 points per line = }
  16.     (0,1,0,4,0,2,1,3,1,5,2,3, {24 numbers}
  17.      2,6,3,7,4,5,4,6,5,7,6,7); {index of the lines, it sets
  18.            how the points are connected together (point #0 to point #1, ...)}
  19. var    xt,yt,zt:real; {temp points}
  20.     xan,yan,zan:real; {angles for axis X,Y,Z}
  21.     sx,sy,sx1,sy1,p,zoom: integer; {Screen coords X,Y}
  22.  
  23. procedure draw(color:byte); {procedure to draw a cube}
  24. begin
  25.  for p:=0 to 11 do begin {loops for all 12 lines}
  26.   sx:=round(zoom*cube[lindex[p*2]*3])+160; {x coord for point #1}
  27.   sy:=round(zoom*cube[lindex[p*2]*3+1])+100; {y coord for point #1}
  28.   sx1:=round(zoom*cube[lindex[p*2+1]*3])+160; {x coord for point #2}
  29.   sy1:=round(zoom*cube[lindex[p*2+1]*3+1])+100; {y coord for point #2}
  30.   drawline(SX,SY,sx1,sy1,color); {draw it in current color}
  31.  end;
  32. end;
  33.  
  34. procedure calc; {calculates new values after rotate step}
  35. begin
  36. for p:=0 to 7 do begin {repeats for all 8 points}
  37. Yt := cube[p*3+1] * COS(Xan) - cube[p*3+2] * SIN(Xan); {read old values from the table of coord,}
  38. Zt := cube[p*3+1] * SIN(Xan) + cube[p*3+2] * COS(Xan); {rotating about x axis}
  39. cube[p*3+1] := Yt; {writes the new values back to the table}
  40. cube[p*3+2] := Zt;
  41.  
  42. Xt := cube[p*3] * COS(Yan) - cube[p*3+2] * SIN(Yan); {same as above, for Y axis}
  43. Zt := cube[p*3] * SIN(Yan) + cube[p*3+2] * COS(Yan);
  44. cube[p*3] := Xt;
  45. cube[p*3+2] := Zt;
  46.  
  47. Xt := cube[p*3] * COS(Zan) - cube[p*3+1] * SIN(Zan); {about Z axis}
  48. Yt := cube[p*3] * SIN(Zan) + cube[p*3+1] * COS(Zan);
  49. cube[p*3] := Xt;
  50. cube[p*3+1] := Yt;
  51. end;
  52. end;
  53.  
  54. begin
  55. Mcgaon;       {sets the 13h (320x200x256) mode}
  56. Zan :=  0.1;  {rotation about Z axis by this angle}
  57. Yan :=  0.02; {rotation about Y axis by this angle}
  58. Xan :=  0.02; {rotation about X axis by this angle}
  59. zoom:=30;     {size of the cube}
  60.  
  61. draw(15);     {draws the cube}
  62. repeat        {loops ...}
  63. draw(0);      {undraws the cube}
  64. calc;        {calculates the new coords}
  65. draw(15);     {draws it again}
  66. delay(30);    {wait a sec!}
  67. until keypressed; {... 'till you press something}
  68. mcgaoff;      {jump back to text mode}
  69. end.